export function formatDate(date, fmt) {
    if (/(y+)/.test(fmt)) {
        fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length));
    }
    let o = {
        'M+': date.getMonth() + 1,
        'd+': date.getDate(),
        'h+': date.getHours(),
        'm+': date.getMinutes(),
        's+': date.getSeconds()
    };
    for (let k in o) {
        if (new RegExp(`(${k})`).test(fmt)) {
            let str = o[k] + '';
            fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? str : padLeftZero(str));
        }
    }
    return fmt;
};

export function changeTime(time) {
    if (time) {
        let oDate = new Date(time * 1)
        let oYear = oDate.getFullYear()
        let oMonth = oDate.getMonth() + 1
        let oDay = oDate.getDate()
        let oHours = oDate.getHours()
        let oMin = oDate.getMinutes()
        let oSec = oDate.getSeconds()
        let oTime = oYear + '-' + getBz(oMonth) + '-' + getBz(oDay) + ' ' + getBz(oHours) +
            ':' + getBz(oMin) + ':' + getBz(oSec)
        return oTime
    } else {
        return ''
    }
}

export function fliDate(time) {
    if (time) {
        let oDate = new Date(time * 1)
        let oYear = oDate.getFullYear()
        let oMonth = oDate.getMonth() + 1
        let oDay = oDate.getDate()
        let oTime = oYear + '年' + getBz(oMonth) + '月' + getBz(oDay) + '日'
        return oTime
    } else {
        return ''
    }
}

export function getYesTime(time) {
    if (time) {
        var yesDate = new Date(time * 1)
        yesDate.setTime(yesDate.getTime() - 24 * 60 * 60 * 1000)
        var oYear = yesDate.getFullYear()
        var oMonth = yesDate.getMonth() + 1
        var oDay = yesDate.getDate()
        var oTime = oYear + '-' + getBz(oMonth) + '-' + getBz(oDay)
        return oTime
    } else {
        return ''
    }
}


function getBz(num) {
    if (parseInt(num) < 10) {
        num = '0' + num
    }
    return num
}

function padLeftZero(str) {
    return ('00' + str).substr(str.length);
};



export function changeDate1(dateNum, format = "yyyy-MM-dd hh:mm:ss") {
    var time = new Date((dateNum - 1) * 24 * 3600000 + 1);
    time.setYear(time.getFullYear() - 70);
    var year = time.getFullYear() + "";
    var month = time.getMonth() + 1 + "";
    var date = time.getDate() + "";
    var hour = (time.getHours() - 8) + "";
    var mins = time.getMinutes() + "";
    var secs = time.getSeconds() + "";

    return format.replace("yyyy", year).replace("MM", (month < 10 ? "0" + month : month))
        .replace("dd", (date < 10 ? "0" + date : date))
        .replace("hh", (hour < 10 ? "0" + hour : hour))
        .replace("mm", (mins < 10 ? "0" + mins : mins))
        .replace("ss", (secs < 10 ? "0" + secs : secs));
}

export function changeDate2(str, format = "yyyy-MM-dd hh:mm:ss") {
    var time = new Date(str);
    var year = time.getFullYear() + "";
    var month = time.getMonth() + 1 + "";
    var date = time.getDate() + "";
    var hour = time.getHours() + "";
    var mins = time.getMinutes() + "";
    var secs = time.getSeconds() + "";
    return format.replace("yyyy", year).replace("MM", (month < 10 ? "0" + month : month))
        .replace("dd", (date < 10 ? "0" + date : date))
        .replace("hh", (hour < 10 ? "0" + hour : hour))
        .replace("mm", (mins < 10 ? "0" + mins : mins))
        .replace("ss", (secs < 10 ? "0" + secs : secs));
}


// 格式化时间（处理new Date()）
export function parseDateTime(time, cFormat) {
    if (arguments.length === 0) {
        return null;
    }
    if (time === undefined || time === null) return "";

    const format = cFormat || "{y}-{m}-{d} {h}:{i}:{s}";
    let date;
    if (typeof time == "object") {
        date = time;
    } else {
        if (("" + time).length === 10) time = parseInt(time) * 1000;
        date = new Date(time);
    }
    const formatObj = {
        y: date.getFullYear(),
        m: date.getMonth() + 1,
        d: date.getDate(),
        h: date.getHours(),
        i: date.getMinutes(),
        s: date.getSeconds(),
        a: date.getDay()
    };
    const time_str = format.replace(/{(y|m|d|h|i|s|a)+}/g, (result, key) => {
        let value = formatObj[key];
        if (key === "a")
            return ["一", "二", "三", "四", "五", "六", "日"][value - 1];
        if (result.length > 0 && value < 10) {
            value = "0" + value;
        }
        return value || 0;
    });
    return time_str;
}

export function getWeekInYear(date) {
        //判断该星期是否跨年，如果跨年就是第一周
    let weekStartDate = getWeekStartByDate(date) //一周开始时间
    let endDate = getDateFromDay(weekStartDate, 6) //一周结束时间
    if (weekStartDate.getFullYear() != endDate.getFullYear()) {
        console.log(endDate.getFullYear() + '年第1周')
        let num = 1
        return num
    }
    let d1 = new Date(date)
    let d2 = new Date(date)
    d2.setMonth(0)
    d2.setDate(1)
    d2 = getWeekStartByDate(d2)
    let rq = d1 - d2
    let days = Math.ceil(rq / (24 * 60 * 60 * 1000)) + 1
    let num = Math.floor(days / 7)
    if (num < 10) {
        return endDate.getFullYear() + "0" + num
    } else {
        return endDate.getFullYear() + "" + num
    }
}

export function getLastMonth(date) {
    let day = new Date(date)
    let y = day.getFullYear()
    let m =  day.getMonth()
    if (m == 0) {
        y--;
        m = 12;
    } 
    return y + '-' + getBz(m);
}

export function getCurrentMonth(date) {
    let day = new Date(date)
    let y = day.getFullYear()
    let m =  day.getMonth() + 1
    return y + '-' + getBz(m);
}


function getWeekStartByDate(date) {
    let day = date.getDay()
    return getDateFromDay(date, -day)
}

function getDateFromDay(dayDate, day) {
    let date = new Date()
    date.setTime(dayDate.getTime() + day * 24 * 60 * 60 * 1000)
    return date
}


// export function getTimestamp(time) { //把时间日期转成时间戳
//     return (new Date(time)).getTime()
// }


// Date.prototype.format = function (fmt) {
//   let o = {
//     'M+': this.getMonth() + 1, // 月份
//     'd+': this.getDate(), // 日
//     'h+': this.getHours(), // 小时
//     'm+': this.getMinutes(), // 分
//     's+': this.getSeconds(), // 秒
//     'q+': Math.floor((this.getMonth() + 3) / 3), // 季度
//     S: this.getMilliseconds() // 毫秒
//   }
//   if (/(y+)/.test(fmt)) {
//     fmt = fmt.replace(RegExp.$1, (this.getFullYear() + '').substr(4 - RegExp.$1.length))
//   }
//   for (let k in o) {
//     if (new RegExp('(' + k + ')').test(fmt)) {
//       fmt = fmt.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k] : ('00' + o[k]).substr(('' + o[k]).length))
//     }
//   }
//   return fmt
// }
// timestampToTime(timestamp) {
//     var date = new Date();//时间戳为10位需*1000，时间戳为13位的话不需乘1000
//     var Y = date.getFullYear() + '-';
//     var M = (date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1) : date.getMonth()+1) + '-';
//     var D = (date.getDate() < 10 ? '0'+date.getDate() : date.getDate()) + ' ';
//     var h = (date.getHours() < 10 ? '0'+date.getHours() : date.getHours()) + ':';
//     var m = (date.getMinutes() < 10 ? '0'+date.getMinutes() : date.getMinutes()) + ':';
//     var s = (date.getSeconds() < 10 ? '0'+date.getSeconds() : date.getSeconds());
//     let strDate = Y+M+D+h+m+s;
//     return strDate;
// },